package Dao;

import DB.DBConnection;
import pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Login {
    public static User login(String username, String password) {
        String sql = "SELECT * FROM users WHERE username =? AND password =?";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
             statement.setString(1, username);
             statement.setString(2, password);
             ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                int  user_id= resultSet.getInt("id");
                return new User(user_id, username, password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}